package com.decent.aop;


import com.decent.service.OperationLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 *  @Aspect：告诉Spring当前类是一个切面类
 */
@Aspect
@Component
public class LogAspects {

    @Autowired
    private OperationLogService operationLogService;

    /**
     * 抽取公共点表达式
     */
    @Pointcut("execution(public Integer com.decent.service.StoreService.*(..))")
    public void pointCut() {}

    @Before("pointCut()")
    public void logStart(JoinPoint joinPoint) {
        operationLogService.insert(joinPoint.getSignature().toString());
    }

    /**
     * JoinPoint 一定要放到开始
     */
    @After("pointCut()")
    public void logEnd(JoinPoint joinPoint) {
        System.out.println("" + joinPoint.getSignature().getName());
    }

    @AfterReturning(value = "pointCut()", returning = "result")
    public void logReturn(Object result) {
        System.out.println("运行结果:{ " + result + " }");
    }

    @AfterThrowing(value = "pointCut()", throwing = "exception")
    public void logException(JoinPoint joinPoint, Exception exception) {
        operationLogService.insert("" + joinPoint.getSignature().getName() + "异常信息剧{ " + exception + "}");
    }
}
